package com.beyondsoft.sabg.edu.model.db.core;


import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import javax.validation.constraints.Size;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * @Author Mo cr
 * @Date 2023/8/4
 */
@Entity
@Data
@EqualsAndHashCode(callSuper = true)
@Table(name = "nothing-user")
public class UserAccount extends BasicEntity implements UserDetails {
    @Column(unique = true)
    private String username;
    private String name;
    private String password;
    @JsonIgnore
    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.REFRESH)
    private Set<Role> roles;

    @Size(max = 20)
    String userType;



    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return roles;
    }

    @Override
    public boolean isAccountNonExpired() {
        return this.getValidity();

    }

    @Override
    public boolean isAccountNonLocked() {
        return this.getValidity();
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.getValidity();

    }

    @Override
    public boolean isEnabled() {
        return this.getValidity();

    }

    public Set<Role> getRoles() {
        if (roles == null) {
            return new HashSet();
        }
        return roles;
    }
}
